package com.zdgcc.site.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Forward {
	/**
	 * 页面跳转
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param str
	 *            要跳转页面路径
	 */
	public static void doForward(HttpServletRequest request,
			HttpServletResponse response, String str) {
		RequestDispatcher rd = null;
		rd = request.getRequestDispatcher(str);
		try {
			rd.forward(request, response);
			return;
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 带返回信息的页面跳转
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param str
	 *            要跳转页面路径
	 * @param message
	 *            返回信息
	 * @return
	 */
	public static void doForward(HttpServletRequest request,
			HttpServletResponse response, String str, String message) {
		RequestDispatcher rd = null;
		rd = request.getRequestDispatcher(str);
		try {
			rd.forward(request, response);
			return;
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 带返回信息的页面跳转，直接跳转
	 * 
	 * @param request
	 * @param response
	 * @param str
	 *            跳转路径
	 * @param message
	 *            信息
	 * @return
	 * @throws IOException
	 */
	public static void doForwardDirect(HttpServletRequest request,
			HttpServletResponse response, String str, String message) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print("<script language=\"javascript\"> alert(\"");
			out.print(message);
			out.println("\");location.href='" + str + "' </script>");

			out.flush();
			out.close();
			return;
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	/**
	 * 页面跳转，直接跳转
	 * 
	 * @param request
	 * @param response
	 * @param str
	 *            跳转路径
	 * 
	 */
	public static void doForwardDirect(HttpServletRequest request,
			HttpServletResponse response, String str) {

		try {
			response.sendRedirect(str);
			return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
